/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tagHandlers;

import DatabaseAccessing.CustomerTransaction;
import DatabaseAccessing.ProductModelController;
import DatabaseAccessing.Products;
import DatabaseAccessing.TransactionModelController;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author MaramZahran
 */
public class CartTagHandler extends SimpleTagSupport {

    private int userID;

    /**
     * Called by the container to invoke this tag. 
     * The implementation of this method is provided by the tag library developer,
     * and handles all tag processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();

        try {
            System.out.println("inside cart handler  " + userID);
            if (userID > 0) {

                // Date today = new Date();
                TransactionModelController tMC = new TransactionModelController();
                ProductModelController pMC = new ProductModelController();
                List<CustomerTransaction> userCart = tMC.getUndoneTransactions(userID);
                CustomerTransaction cT = null;

                if (userCart != null) {
                    // List<CustomerTransaction> cartList = Collections.EMPTY_LIST;
                    System.out.println("Inside the cart ***" + userCart.size());
                    float total = 0;
                    int items = 0;
                    for (int i = 0; i < userCart.size(); i++) {

                        cT = (CustomerTransaction) userCart.get(i);
                        //cartList.add(cT);
                        total = total + (cT.getCount() * cT.getUnitPrice());
                        items = items + cT.getCount();
                        /*out.println("<div class=\"form_row\">");
                        Products pro = pMC.viewProductByID(cT.getProductId());
                        out.println("Name: " + pro.getProductName() + "<br/>");
                        out.println("Price: " + pro.getPrice());
                        out.println("Count: " + cT.getCount() + "<br/>");
                        out.println("<a href=\"RemoveFromCart?ProductID=" + pro.getProductId().toString() + "\"> Remove from cart</a>");
                        out.println("</div>");*/
                    }
                    out.println("<div class=\"cart_details\">" + items + "items <br /><span class=\"border_cart\"></span>Total: <span class=\"price\">" + total + "$</span></div>");
                    // getJspContext().setAttribute("allproductinCarts", userCart);
                }
            } else {
                System.out.println("inside else show cart handler");
                float total = 0;
                int items = 0;
               // Object cartObj = getJspContext().getAttribute("Cart");
                Object cartObj =  getJspContext().getAttribute("Cart", PageContext.SESSION_SCOPE);
                System.out.println("After Object(((((((((((((((((( ");
                if (cartObj != null) {
                    List<CustomerTransaction> cartList = (List) cartObj;
                    System.out.println("***************Size"+cartList.size());
                    for (int i = 0; i < cartList.size(); i++) {
                        //cartList.add(cT);
                        total = total + (cartList.get(i).getCount() * cartList.get(i).getUnitPrice());
                        items = items + cartList.get(i).getCount();
                        System.out.println("Items"+items);

                    }
                }
                else
                {
                    System.out.println("NULLLLLLLLLLLLLLLLLLLL");
                }
                out.println("<div class=\"cart_details\">" + items + "items <br /><span class=\"border_cart\"></span>Total: <span class=\"price\">" + total + "$</span></div>");
            }

            // TODO: insert code to write html before writing the body content.
            // e.g.:
            //
            // out.println("<strong>" + attribute_1 + "</strong>");
            // out.println("    <blockquote>");

            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }

            // TODO: insert code to write html after writing the body content.
            // e.g.:
            //
            // out.println("    </blockquote>");

        } catch (java.io.IOException ex) {
            throw new JspException("Error in CartTagHandler tag", ex);
        }
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }
}
